<!-- subpkg_medicine/order_list/index.vue -->
<script lang="ts" setup>
  import { orderListApi } from '@/apis/order'
  import { onMounted, ref } from 'vue'

  const feedTabs = ref([
    { label: '全部', rendered: true },
    { label: '待支付', rendered: false },
    { label: '待发货', rendered: false },
    { label: '待收货', rendered: false },
    { label: '已完成', rendered: false },
  ])
  // 订单列表
  const orderList = ref([])
  // 标签页切换
  function onTabClick(index) {
    let status = 12
    if (index === 0) {
      getAll()

      return
    } else if (index === 1) {
      status = 11
    } else if (index === 2) {
      status = 12
    } else if (index === 3) {
      status = 13
    } else if (index === 4) {
      status = 14
    }
    console.log(status)
    // 获取订单列表
    getOrderList(status)
  }

  // 加载状态
  const loading = ref(true)
  // 列表是否为空
  const isEmpty = ref(false)

  // 药品订单列表
  async function getOrderList(status) {
    // 显示加载状态
    loading.value = true

    // 列表是否为空

    isEmpty.value = false

    // 订单列表接口
    const data = await orderListApi(status)
    console.log(data)
    // 渲染列表数据
    orderList.value = data.rows

    // 关闭加载状态
    loading.value = false
    // 列表是是否为空
    isEmpty.value = orderList.value.length === 0
  }
  // 获取全部订单
  const getAll = async () => {
    loading.value = true
    isEmpty.value = false

    const list1 = await orderListApi(10)
    const list2 = await orderListApi(11)
    const list3 = await orderListApi(12)
    const list4 = await orderListApi(13)
    const list5 = await orderListApi(14)
    orderList.value.push(...list1.rows, ...list2.rows, ...list3.rows, ...list4.rows, ...list5.rows)
    loading.value = false
    isEmpty.value = orderList.value.length === 0
  }
  // 获取药品订单列表
  getAll()
</script>
<template>
  <view class="medicine-page">
    <view class="order-status-tabs">
      <view class="order-status-tabs">
        <custom-tabs @change="onTabClick" :data="feedTabs"></custom-tabs>
      </view>
    </view>
    <scroll-view refresher-enabled refresher-background="#f6f6f6" class="uni-scroll-view" scroll-y>
      <view class="order-list">
        <view v-for="order in orderList" :key="order.id" class="order-list-item">
          <view class="order-header">
            <text class="number">订单编号: {{ order.id }}</text>
            <text class="status">待支付</text>
          </view>
          <navigator
            :url="`/subpkg_medicine/order_detail/index?id=${order.id}`"
            hover-class="none"
            class="order-body"
          >
            <view v-for="medicine in order.medicines" :key="medicine.id" class="medicine">
              <image class="medicine-cover" :src="medicine.avatar" mode="aspectFill" />
              <view class="medicine-info">
                <text class="name">{{ medicine.name }}</text>
                <text :class="{ symbol: medicine.prescriptionFlag === 1 }" class="unit">
                  {{ medicine.specs }}
                </text>
                <text class="price">¥{{ medicine.amount }}</text>
              </view>
              <view class="quantity">x{{ medicine.quantity }}</view>
            </view>
          </navigator>
          <view class="order-footer">
            <view class="total"> 共2件商品 合计：¥{{ order.actualPayment }}（运费¥0.00） </view>
            <view class="buttons">
              <button class="uni-button minor">取消订单</button>
              <button class="uni-button">继续支付</button>
            </view>
          </view>
        </view>

        <!-- 列表数据为空 -->
        <view v-if="isEmpty" class="empty-tips">没有更多数据了</view>

        <!-- 加载状态 -->
        <uni-load-more
          v-show="loading"
          status="loading"
          color="#C3C3C5"
          :icon-size="16"
          :content-text="{
            contentdown: '上拉显示更多',
            contentrefresh: '数据正在加载中',
            contentnomore: '没有更多数据了',
          }"
        />
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
  @import './index.scss';
  .empty-tips {
    margin-top: 40rpx;
    text-align: center;
    font-size: 28rpx;
    color: #c3c3c5;
  }
</style>
